package _20250228;

import java.io.File;
import java.io.IOException;


public class SearchQQ {
    /**
     * 在F盘搜索qq音乐并启动  F:/1-ruanjian/QQMusic/QQMusic.exe
     */
    public static void main(String[] args) throws IOException {

//        File qqMusic=new File("F:/1-ruanjian");
//        File qqmusicFile=searchQQMusic(qqMusic,"QQMusic.exe");
//        System.out.println("目录是"+qqmusicFile);
        File qqMusic=new File("F:/1-ruanjian/QQMusic");
        File qqmusicFile=searchQQMusic(qqMusic,"QQMusic.exe");
        System.out.println("目录是"+qqmusicFile);
        if (qqmusicFile!=null) {
            try {
                Runtime runtime=Runtime.getRuntime();
                Process pb=runtime.exec(qqmusicFile.getAbsolutePath());
            } catch (IOException e) {
                System.err.println("启动qq音乐时出错"+e.getMessage());;
            }
        }else {
            System.out.println("未找到qq音乐");
        }
    }

    /**
     * 递归查找项目
     * @param directory 目录
     * @param s 文件名字
     * @return  返回文件名
     */
    private static File searchQQMusic(File directory, String s) {
        //1.先判断文件下有无目录，没有则返回null
        if (directory.isDirectory()){
            System.out.println("1"+directory);
            //2.将该文件下的目录进入文件组方便查询
            File[] files=directory.listFiles();
            //3.如果文件不为空，进行遍历操作
            if (files!=null) {
                //4.遍历
                for (File file:files) {
                    System.out.println("2"+file);
                    //5.判断有无目录
                    if(file.isDirectory()){
                        System.out.println("3"+file);
                        //6.递归调用
                        File result=searchQQMusic(file,s);
                        if(result!=null){
                            System.out.println("4"+result);
                            return result;
                        }
                    } else if (file.getName().equalsIgnoreCase(s)) {
                        System.out.println("5"+file);
                        return file;
                    }
                }
                }
            }
        System.out.println("--------------------");
        return null;
    }


}
